# -*- coding: utf-8 -*-
"""
Created on Tue Sep  6 15:28:26 2022

@author: wulong
"""
import numpy as np
from casadi import *
from Day_formulate_Opt import *
from Obtain_results import *

def D_OPT(initial_state, state_guess, state_bnds_lo, state_bnds_up,
          input_cont_guess, input_cont_bnds_lo, input_cont_bnds_up,
          input_cont_lbg, input_cont_ubg,
          input_bin_guess, input_bin_bnds_lo, input_bin_bnds_up,
          distb_bnds,
          output_guess,
          y2_setpnts_lo, y2_setpnts_up,
          output_bnds_lo, output_bnds_up,
          alpha, alpha_slack, pred_horzn,
          pf, pmg, pse, i,
          pcm, ppn, rxi, rxi_as):
    
    distb_bnds_new = distb_bnds[i:i+pred_horzn,:]
    
    y2_setpnts_lo_new = y2_setpnts_lo[i:i+pred_horzn]
    y2_setpnts_up_new = y2_setpnts_up[i:i+pred_horzn]
    
    pse_new = pse[i:i+pred_horzn]
    pcm_new = pcm[i:i+pred_horzn]
    ppn_new = ppn[i:i+pred_horzn]
    rxi_new = rxi[i:i+pred_horzn]
    rxi_as_new = rxi_as[i:i+pred_horzn]
    
    f_opt = formulate_opt_h(initial_state, state_guess, state_bnds_lo, state_bnds_up,
                            input_cont_guess, input_cont_bnds_lo, input_cont_bnds_up,
                            input_cont_lbg, input_cont_ubg,
                            input_bin_guess, input_bin_bnds_lo, input_bin_bnds_up,
                            distb_bnds_new,
                            output_guess,
                            y2_setpnts_lo_new, y2_setpnts_up_new,
                            output_bnds_lo, output_bnds_up,
                            alpha, alpha_slack, pred_horzn,
                            pf, pmg, pse_new, 
                            pcm_new, ppn_new, rxi_new, rxi_as_new)
    
    s_opt = solve_opt_h(*f_opt)
    
    r_opt = get_results_h(s_opt, pred_horzn)
    
    return r_opt
